package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.DishDTO;
import com.tm.entity.DishEntity;
import com.tm.service.DishService;
import com.tm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-05 10:31
 */
@RestController
@RequestMapping("dish")
@CrossOrigin
public class DishController {


    @Autowired
    private DishService dishService;

    @PostMapping("page")
    public Result page(@RequestParam(required = false,defaultValue = "1") Integer page,
                       @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                       DishDTO dishDTO){
        Page<DishEntity> dishEntityPage = dishService.pageList(page, pageSize, dishDTO);
        return Result.success(dishEntityPage);
    }

    @DeleteMapping
    public Result deleteDish(@RequestParam("ids") List<Long> ids){
        Boolean flag = dishService.removeByIds(ids);
        return Result.success(flag);
    }

    @PostMapping
    public Result addCategory(@RequestBody DishEntity dishEntity){
        Boolean flag = dishService.addCategory(dishEntity);
        return Result.success(flag);
    }

    @PostMapping("/status")
    public Result dishStatusByStatus(@RequestBody List<Long> ids){
        Boolean flag = dishService.updateIds(ids);
        return Result.success(flag);
    }

    @GetMapping("/{id}")
    public Result getDishList(@PathVariable("id") Long id){
        DishEntity dishEntity = dishService.getById(id);
        return Result.success(dishEntity);
    }

    @GetMapping("list")
    public Result queryDishList(){
        List<DishEntity> dishEntity = dishService.list();
        return Result.success(dishEntity);
    }

    @PutMapping
    public Result editOrderDetail(@RequestBody DishEntity dishEntity){
        UpdateWrapper<DishEntity> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id",dishEntity.getId());
        Boolean flag = dishService.update(dishEntity,UpdateWrapper);
        return Result.success(flag);
    }



}
